﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Xsl;
using System.Xml;
using System.IO;
using System.Data;

namespace scrumbench
{
    public partial class WorkbookEngine
    {
        public void ExportToCsvFromDataSet(DataSet dsExport)
        {

        
           

                StringBuilder dataToExport = new StringBuilder();

                foreach (DataTable dtExport in dsExport.Tables)
                {

                    string headerToExport = string.Empty;

                    foreach (DataColumn dCol in dtExport.Columns)

                        headerToExport = (char)34 + dCol.ColumnName + (char)34 + (char)44;

                    headerToExport.Remove(headerToExport.Length - 1, 1);

                    headerToExport = headerToExport + Environment.NewLine + Environment.NewLine;

                    dataToExport.Append(headerToExport);

                    string bodyToExport = string.Empty;

                    foreach (DataRow dRow in dtExport.Rows)
                    {

                        foreach (object obj in dRow.ItemArray)

                            bodyToExport = bodyToExport + obj.ToString() + (char)44;

                        bodyToExport.Remove(bodyToExport.Length - 1, 1);

                        bodyToExport = bodyToExport + Environment.NewLine;

                    }

                    dataToExport.Append(bodyToExport);

                    dataToExport.Append(Environment.NewLine);

                    dataToExport.Append(Environment.NewLine);

                    if (string.IsNullOrEmpty(dataToExport.ToString()))
                    {

                        System.IO.StreamWriter file = new System.IO.StreamWriter("c:\\test.csv");
                        file.WriteLine(dataToExport);

                        file.Close();

                        //Response.Clear();

                        //Response.ContentType = "Text/vnd.ms-excel";

                        //Response.AddHeader("Content-Disposition", "attachment;filename=report.csv");

                        //Response.Write(dataToExport.ToString());

                        //IsOutputStreamed = true;

                    }

                }

            


        }
    }
}